package messageAPI.Core;

import java.util.*;

/**
 * The Class PerformanceEvent.
 */
public class PerformanceEvent {
	//test.latency 132465798 122 test=test2 test3=test4
	
    public String Category;
    public long TimeStamp;
    public long Value;

    public Map<String, String> ContextValues;
    
    /**
     * Instantiates a new performance event.
     *
     * @param category the category
     * @param timeStamp the time stamp
     * @param value the value
     */
    public PerformanceEvent(String category, long timeStamp, long value)
    {
    	this.Category=category;
    	this.TimeStamp=timeStamp;
    	this.Value=value;
    	
    	ContextValues = new HashMap<String, String>();
    }

    /**
     * Adds the context value.
     *
     * @param contextId the context id
     * @param contextValue the context value
     */
    public void addContextValue(String contextId, String contextValue)
    {
    	this.ContextValues.put(contextId,contextValue);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public java.lang.String toString() {

    	StringBuilder builder = new StringBuilder();

        builder.append(this.Category);
        builder.append(" ");
        builder.append(this.TimeStamp);
        builder.append(" ");
        builder.append(this.Value);

        for(String key:ContextValues.keySet())
        {
            builder.append(" ");
            builder.append(key);
            builder.append("=");
            builder.append(ContextValues.get(key));
        }
    	
    	return builder.toString();
    }
}




//OLD CODE WHICH MUST BE DELETED AFTER INTEGRATION TESTING

//    /**
//     * Instantiates a new performance event.
//     */
//    public PerformanceEvent()
//    {
//        ContextValues = new HashMap<String, String>();
//    }


//    /**
//     * From string.
//     *
//     * @param data the data
//     * @return the performance event
//     * @throws Throwable the throwable
//     */
//    public static PerformanceEvent fromString(String data) throws Throwable
//    {
//        if (data.isEmpty() || data==null)
//        {
//            throw new Exception("Input string is in the incorrect format.");
//        }
//
//        PerformanceEvent performanceEvent = new PerformanceEvent();
//
//        String[] performanceData = data.split(" ");
//
//        if (performanceData.length < 4)
//        {
//            throw new Exception("Input string is in the incorrect format.");
//        }
//
//        performanceEvent.Category = performanceData[0];
//
//        long timeStamp = 0;
//
//        timeStamp=Long.parseLong(performanceData[1]);
//
//        if (timeStamp==0)
//        {
//            throw new Exception("Input string is in the incorrect format.");
//        }
//
//        performanceEvent.TimeStamp = timeStamp;
//
//        long perfValue = 0;
//        perfValue=Long.parseLong(performanceData[1]);
//
//        if (perfValue==0)
//        {
//            throw new Exception("Input string is in the incorrect format.");
//        }
//
//        performanceEvent.Value = perfValue;
//
//        for (int i = 3; i < performanceData.length; i++)
//        {
//            String[] contextItem = performanceData[i].split("=");
//            if (contextItem.length != 2)
//            {
//                throw new Exception("Input string is in the incorrect format.");
//            }
//
//            performanceEvent.ContextValues.put(contextItem[0], contextItem[1]);
//        }
//
//        return performanceEvent;
//    }




